package vsy.buzzwordbingo.controller;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import vsy.buzzwordbingo.model.Game;
import vsy.buzzwordbingo.model.GameCore;

@Controller
public class NewGameController {

	@Resource
	private GameCore gameCore;
	
	@Resource
	private Long ajaxGamePollInterval;

	@Resource
	private BigInteger minGameSize;

	@Resource
	private BigInteger maxGameSize;

	@RequestMapping(value = "/buzzwords", method = RequestMethod.POST)
	public ModelAndView newGame(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("title") String titleParam,
			@RequestParam("size") String sizeParam,
			@RequestParam("description") String descriptionParam) {

		request.setAttribute("title", titleParam);
		request.setAttribute("size", sizeParam);
		request.setAttribute("description", descriptionParam);
		
		if (!validateParams(request, titleParam, sizeParam)) {
			request.setAttribute("minGameSize", minGameSize);
			request.setAttribute("maxGameSize", maxGameSize);
			request.setAttribute("maxGameSizeLength", maxGameSize.toString().length());
			return new ModelAndView("home");
		}

		int size = Integer.parseInt(sizeParam);
		int buzzwordCount = (size % 2 == 0) ? size * size : size * size - 1;
		
		request.setAttribute("buzzwordCount", buzzwordCount);
		return new ModelAndView("buzzwords");
	}
	
	@RequestMapping(value = "/game", method = RequestMethod.POST)
	public String postGame(HttpServletRequest request,
			HttpServletResponse response, @RequestParam String title, @RequestParam int size, @RequestParam String description) {
		
		String playerName = request.getUserPrincipal().getName();
		
		List<String> buzzwords = getBuzzwords(request);
		
		Game newGame = null;
		newGame = gameCore.createGame(playerName, title, size, description, buzzwords);
		
		return "redirect:/game/" + newGame.getId();
//		request.setAttribute("player", playerName);
//		request.setAttribute("game", newGame);
//		request.setAttribute("pollInterval", ajaxGamePollInterval);
//		return new ModelAndView("game");
	}
	
	private List<String> getBuzzwords(HttpServletRequest request) {
		List<String> buzzwords = new ArrayList<String>();
		@SuppressWarnings("unchecked")
		Enumeration<String> parameterNames = request.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String parameterName = parameterNames.nextElement();
			if (parameterName.startsWith("buzzword_")) {
				//TODO check if there are duplicate buzzwords
				buzzwords.add(request.getParameter(parameterName));
			}
		}
		return buzzwords;
	}
	
	@RequestMapping(value = "/json/test", method = RequestMethod.GET)
	public @ResponseBody Foo getGameAsJSON(HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("application/json");
		Foo foo = new Foo();
		Bar bar = new Bar();
		foo.setBarVar(bar);
		bar.setFooVar(foo);
		return foo;
	}
	private class Foo {
		private Bar bar;
		public Bar getBarVar() {
			return bar;
		}
		
		public void setBarVar(Bar bar) {
			this.bar = bar;
		}
	}
	
	private class Bar {
		private Foo foo;
		public Foo getFooVar() {
			return foo;
		}
		
		public void setFooVar(Foo foo) {
			this.foo = foo;
		}
	}

	private boolean validateParams(HttpServletRequest request,
			String titleParam, String sizeParam) {
		boolean titleError = false;
		boolean sizeError = false;
		if (titleParam == null || titleParam.trim().length() == 0) {
			request.setAttribute("titleErrorMsg", "home.titleError.titleEmpty");
			titleError = true;
		}
		if (sizeParam == null || sizeParam.trim().length() == 0) {
			request.setAttribute("sizeErrorMsg", "home.sizeError.sizeEmpty");
			sizeError = true;
		}
		if (!sizeError && !sizeParam.matches("\\d+")) {
			request.setAttribute("sizeErrorMsg",
					"home.sizeError.sizeNotANumber");
			sizeError = true;
		}
		if (!sizeError) {
			BigInteger size = new BigInteger(sizeParam);
			if (size.compareTo(minGameSize) == -1) {
				request.setAttribute("sizeErrorMsg",
						"home.sizeError.sizeBelowMin");
				sizeError = true;
			} else if (size.compareTo(maxGameSize) == 1) {
				request.setAttribute("sizeErrorMsg",
						"home.sizeError.sizeAboveMax");
				sizeError = true;
			}
		}
		request.setAttribute("titleError", titleError);
		request.setAttribute("sizeError", sizeError);
		return !titleError && !sizeError;
	}
}
